package com.sfx.算法专题.双指针.同向双指针.普通双指针;

/**
 * Created with IntelliJ IDEA.
 * Description:https://leetcode.cn/problems/duplicate-zeros/
 * User: sfx
 * Date: 2023-07-18
 * Time: 12:27
 */
public class Code_3_duplicateZeros {
    static class Solution {
        public void duplicateZeros(int[] arr) {
            int n = arr.length;
            int cur = 0;
            int dest = 0;
            //1.先找到截断位置
            while(dest < n) {
                if(arr[cur] == 0) {
                    dest++;
                }
                cur++;
                dest++;
            }
            cur--;
            dest--;
            //2.从截断位置,从后往前复写
            while(cur>=0&&dest>=0) {
                if(dest<n) {
                    arr[dest]=arr[cur];
                }
                if(arr[cur] == 0 && --dest>=0) {
                    arr[dest] = 0;
                }
                dest --;
                cur --;
            }
        }
    }
}
